#include<iostream>
#include<string>
using namespace std;
int ages(string birth){
	// 判断年龄的差值

	int year=atoi((birth.substr(0,4)).c_str());
	int month=atoi((birth.substr(5,7)).c_str());
	int day=atoi((birth.substr(8,10)).c_str());
	int ages=(year-2014)*365+(month-9)*30+(day-6);
	return -ages;

}
int main(){
	freopen("input.txt","r",stdin);
	int N;
	cin>>N;
	string name,birth;
	int sum=0;
	int  ages_max=0;
	int ages_min=200*365;
	string name_max,name_min;

	while(N--){
		cin>>name>>birth;
		if(ages(birth)<=200*365&&ages(birth)>0){
			sum++;
			if(ages(birth)>ages_max){
				ages_max=ages(birth);
				name_max=name;
			}
			if(ages(birth)<ages_min){
				ages_min=ages(birth);
				name_min=name;
			}
		}
	}
	cout<<sum<<" "<<name_max<<" "<<name_min<<endl;
	return 0;

	return 0;
}